
package com.ido.babyshow.util;

import java.io.IOException;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * Json通用类,使用Jackson来实现.
 * 
 * @author Baishuang
 */
public class JsonUtil
{

    protected final static Log log = LogFactory.getLog(JsonUtil.class);

    /**
     * @param obj
     *            要转json的对象
     * @return JSON 串
     */
    public static String getJson(Object obj)
    {
        ObjectMapper mapper = new ObjectMapper();
        try
        {
            return mapper.writeValueAsString(obj);
        }
        catch (JsonGenerationException e)
        {
            log.error(e);
        }
        catch (JsonMappingException e)
        {
            log.error(e);
        }
        catch (IOException e)
        {
            log.error(e);
        }
        return "";
    }

    /**
     * 通过JSON串得到对象
     * 
     * @param jsonStr
     *            json对象
     * @param clazz
     *            返回对象的类型
     * @return 得到的json的对象
     */
    public static <T> T getObject(String jsonStr, Class<T> clazz)
    {
        if (jsonStr == null || jsonStr.trim().isEmpty())
        {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        try
        {
            return mapper.readValue(jsonStr, clazz);
        }
        catch (JsonParseException e)
        {
            log.error(e);
        }
        catch (JsonMappingException e)
        {
            log.error(e);
        }
        catch (IOException e)
        {
            log.error(e);
        }
        return null;
    }

    /**
     * 把对象转换成json，写到out流里面
     * @param out
     * @param obj
     */
    public static void writeJson(Writer out, Object obj)
    {
        if (obj != null && out != null)
        {
            try
            {
                out.write(getJson(obj));
                out.flush();
            }
            catch (IOException e)
            {
                log.error(e);
            }
            finally
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    log.error(e);
                }
            }
        }

    }

}
